<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------

namespace app\common\logic\view;

use \think\Db;

class ViewLogLogic
{
    /**
     * User:Simony
     * Function: 添加查看记录
     * @param $member_id
     * @param $been_member_id
     * @param $type 查看类型：1，会员信息 2微信+聊天社交 3，相册
     */
    public function addViewLog($member_id, $been_member_id, $config_name, $price = 0)
    {
        if ($member_id == $been_member_id) return ds_arr(SUCCESS, '自己查看自己无需添加记录');
        $levelModel = model('common/member/Level');

        if (is_string($config_name)) {
            switch ($config_name) {
                case $levelModel::VIEW_MEMBER_EFFECTIVE_TIME:
                    $type = 1; //1，会员信息
                    break;
                case $levelModel::DAY_VIEW_MEMBER_SOCIAL_ACCOUNT_EFFECTIVE_TIME:
                    $type = 2; //2微信+聊天社交
                    break;
                case $levelModel::DAY_VIEW_MEMBER_ALBUM_EFFECTIVE_TIME:
                    $type = 3; //3，相册
                    break;
                default:
                    return ds_arr(ERROR, '类型必须是1，会员信息 2微信+聊天社交 3，相册');
                    break;
            }
        } elseif (is_int($config_name)) {
            $type = $config_name;
            switch ($type) {
                case 1:
                    $config_name = $levelModel::VIEW_MEMBER_EFFECTIVE_TIME; //1，会员信息
                    break;
                case 2:
                    $config_name = $levelModel::DAY_VIEW_MEMBER_SOCIAL_ACCOUNT_EFFECTIVE_TIME; //3，相册
                    break;
                case 3:
                    $config_name = $levelModel::DAY_VIEW_MEMBER_ALBUM_EFFECTIVE_TIME; //3，相册
                    break;
                default:
                    return ds_arr(ERROR, '类型必须是1，会员信息 2微信+聊天社交 3，相册');
                    break;
            }
        }

        $current_time = time();
        $data = [
            'member_id' => $member_id,
            'been_member_id' => $been_member_id,
            'type' => $type,
            'view_time' => $current_time,
            'price' => $price
        ];

        $authLogic = model('common/AuthLogic', 'logic');
        $config_day = $authLogic->getExpireConfig($member_id, $config_name);
        $data['expire_time'] = $current_time + $config_day * 86400;

        $res = model('common/view/ViewLog')->add($data);
        return $res ? ds_arr(SUCCESS, '添加查看日志成功', ['id' => $res]) : ds_arr(ERROR, '添加查看日志失败');
    }

}